package com.we823.cms.repository.service.extension;

import java.util.List;

import javax.annotation.Resource;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.we823.cms.model.extension.Brand;
import com.we823.cms.model.extension.ProductCategory;
import com.we823.cms.model.extension.Product;
import com.we823.cms.repository.dao.extension.BrandMapper;
import com.we823.cms.repository.dao.extension.ProductMapper;
import com.we823.cms.repository.service.AbstractBaseService;
import tk.mybatis.mapper.entity.Example;

@Service("productService")
@Scope("prototype")
public class ProductService extends AbstractBaseService<Product, Integer> {

	private ProductMapper productMapper;
	
	private @Resource BrandMapper brandMapper;
	
	@Autowired
	public ProductService(ProductMapper productMapper) {
		super(productMapper);
		this.productMapper = productMapper;
	}

	public List<Product> findByCategory(List<ProductCategory> productCategories) {
		if (productCategories==null) return null;
		List<Integer> categories = Lists.newArrayList();
		for (ProductCategory productCategory : productCategories) {
			categories.add(productCategory.getId());
		}
		Example example = new Example(Product.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("categoryId", categories);
		return productMapper.selectByExample(example);
	}

	public List<Product> findByCategory(ProductCategory category) {
		if (category==null) return null;
		Product product = new Product();
		product.setCategoryId(category.getId());
		return productMapper.select(product);
	}

	public Product findByCode(String productCode) {
		Product product = new Product();
		product.setCode(productCode);
		List<Product> products = productMapper.select(product);
		if (products!=null && products.size()>0){
			return products.get(0);
		}
		return null;
	}

	public List<Product> findByKeyword(String keyword) {
		List<Brand> brandModels ;
		String lowKeyword ;
		if (!StringUtils.isEmpty(keyword)) {
			lowKeyword = keyword.toLowerCase();

			Example example = new Example(Brand.class);
			Example.Criteria criteria = example.createCriteria();
            criteria.andLike("name", lowKeyword);
			brandModels = brandMapper.selectByExample(example);
			List<Integer> brandIds = Lists.newArrayList();
			if (brandModels!=null && brandModels.size()>0) {
				for (Brand brandModel : brandModels) {
					brandIds.add(brandModel.getId());
				}
			}
			if (brandIds.size()>0) {
				example = new Example(Product.class);
				criteria = example.createCriteria();
				criteria.andLike("name", lowKeyword);
				criteria.andIn("brandId", brandIds);
				return productMapper.selectByExample(example);
			}else {
				example = new Example(Product.class);
				criteria = example.createCriteria();
				criteria.andLike("name", lowKeyword);
				return productMapper.selectByExample(example);
			}
		}
		return null;
	}

	public PageInfo<Product> findByCategoryAndBrand(ProductCategory productCategoryModel, Brand brandModel, PageRequest pageRequest) {
		setPageInfo(pageRequest);
		Product product = new Product();
		product.setCategoryId(productCategoryModel.getId());
		product.setBrandId(brandModel.getId());
		return new PageInfo<>(productMapper.select(product));
	}

	public PageInfo<Product> findByCategory(ProductCategory productCategoryModel, PageRequest pageRequest) {
		setPageInfo(pageRequest);
		Product product = new Product();
		product.setCategoryId(productCategoryModel.getId());
		return new PageInfo<>(productMapper.select(product));
	}

	public PageInfo<Product> findByBrand(Brand brandModel, PageRequest pageRequest) {
		setPageInfo(pageRequest);
		Product product = new Product();
		product.setBrandId(brandModel.getId());
		return new PageInfo<>(productMapper.select(product));
	}

}
